import logging

import pytest

from resource.pom.settings import Settings
from utils.manager import Direction, MatchRules


class Test:
    app = Settings

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.点击设置-用户和帐号-当前登录')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.dirc_fling(Direction.Down)
        device.click(*self.app.users_accounts_item)
        device.refresh_layout()
        if device.get_element_by_text('user'):
            device.click_element(device.get_element_by_text('当前登录'))
        else:
            device.click_element(device.get_element_by_text('当前登录'))
            device.refresh_layout()
            device.click_element(device.get_element_by_text('user'))
            device.refresh_layout()
            device.click_element(device.get_element_by_text('确定'))
            device.wait(5)
            device.start_ability(self.app.bundle_name, self.app.ability_name)
        logging.info('预期结果1.可正常进入到当前登录界面（若无"新用户"需要先添加一个新用户）')
        device.refresh_layout()
        device.assert_text_exist('用户')
        device.assert_text_exist('添加用户')
        device.assert_text_exist('添加访客')
        if not device.get_element_by_text('test'):
            device.click_element(device.get_element_by_text('添加用户'))
            device.refresh_layout()
            device.click_element(device.get_element_by_type('TextInput'))
            # 输入框中输入文本
            device.input_text(80, 465, 'test')
            # 点击添加
            device.click(523, 580)
            device.wait(5)
            device.click(200, 1117)
            device.wait(1)
            device.refresh_layout()
        logging.info('步骤2.1点击已添加的新用户')
        new_user = device.get_element_by_text('test')
        device.click_element(new_user)
        logging.info('预期结果2.1点击已添加的新用户可正常弹出弹窗')
        device.refresh_layout()
        device.assert_text_exist('切换用户')
        device.assert_text_exist('删除用户')

        logging.info('步骤2.2点击删除用户')
        device.click_element(device.get_element_by_text('删除用户'))
        logging.info('预期结果2.2点击删除用户可正常弹出删除确认的弹窗')
        device.refresh_layout()
        device.assert_text_exist('删除该用户并移除所有用户数据', rule=MatchRules.Contain)
        device.assert_text_exist('取消')
        device.assert_text_exist('删除')

        logging.info('步骤3.点击删除')
        device.click_element(device.get_element_by_text('删除'))
        logging.info('预期结果3.点击删除可以正常删除用户')
        device.wait(6)
        device.refresh_layout()
        device.assert_text_exist('user')
        device.assert_text_exist('test', expect=False)
